package com.sqkj.controller;

import com.sqkj.entity.Article;
import com.sqkj.entity.User;
import com.sqkj.entity.UserCard;
import com.sqkj.service.UserService;
import com.sqkj.service.UserCardService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/UserCard")
@RestController
public class UserCardController {
    @Resource
    UserCardService service;
    @Resource
    UserService userService;
    @RequestMapping("/add")
    public Map<String, Object> add(UserCard user) {
        Map<String, Object> map = new HashMap<String, Object>();
        String userIcon = user.getUserCardIcon();
        String type = userIcon.indexOf(".") != -1 ? userIcon.substring(userIcon.lastIndexOf(".") + 1, userIcon.length()) : null;
        if (type != null && !"".equals(type)) {
            if ("GIF".equals(type.toUpperCase())||"PNG".equals(type.toUpperCase())||"JPG".equals(type.toUpperCase()) ||"JPEG".equals(type.toUpperCase())) {
                String userIcon2 = userIcon.substring(userIcon.lastIndexOf("/")+1);     //得到图片名
                String userIcon3 = "http://localhost:8080/Path2/UserIcon/" + userIcon2;
                user.setUserCardIcon(userIcon3);
                return addUser(user);
            }else {
                map.put("myCard", "只支持图片格式");
                return map;

            }
        } else {
            map.put("myCard", "文件类型为空");
            return map;
        }

    }
    public Map<String, Object> addUser(UserCard user){
        Map<String, Object> map = new HashMap<String, Object>();
        if(user !=null && !"".equals(user.getJob() ) &&  !"".equals(user.getPhone() )&& !"".equals(user.getAddress()) &&!"".equals(user.getComName()) &&!"".equals(user.getEmail()) &&!"".equals(user.getComInfo()) &&!"".equals(user.getUsComName() )){
            service.add(user);

            map.put("myCard", true );
            return map;
        }else {
            map.put("myCard", null);
            return map;
        }
    }
    @RequestMapping("/findAll")
    public Map<String,Object> findAll() {
        Map<String, Object> map = new HashMap<String, Object>();
        List<UserCard> list = service.findAll();
        for (UserCard userCard:list) {
            userCard.setUser(userService.findById(userCard.getUserId()));
        }
        map.put("Users",list );
        return map;
    }
    @RequestMapping("/findById")
    public Map<String,Object> findById(int id) {
        Map<String, Object> map = new HashMap<String, Object>();
        UserCard userCard = service.findById(id);
        userCard.setUserId(userCard.getUser().getId());
        map.put("UserCard",userCard );
        return map;
    }
    @RequestMapping("/deleteById")
    public Map<String,Object> deleteById(int id) {
        Map<String, Object> map = new HashMap<String, Object>();
        Boolean isSuccess = service.deleteById(id);
        map.put("isSuccess", isSuccess);
        return map;
    }
    @RequestMapping("/findByUserId")
    public Map<String, Object> findByUserId(int userId) {
        Map<String, Object> map = new HashMap<>();
        List<UserCard> userCardList = service.findByUserId(userId);
        for (UserCard userCard:userCardList) {
            userCard.setUser(userService.findById(userCard.getUserId()));
        }
        map.put("userCardList", userCardList);
        return map;
    }
    @RequestMapping("/findByUserId2")
    public Map<String, Object> findByUserId2(Integer userId) {
        Map<String, Object> map = new HashMap<>();
        List<UserCard> userCardList= service.findByUserId(userId);

        if(userCardList.size() == 0){
            map.put("isExist", false);
            return map;
        }else {
            for (UserCard userCard:userCardList) {
                userCard.setUser(userService.findById(userCard.getUserId()));
            }
            map.put("isExist", true);

            return map;
        }

    }
    @RequestMapping("/findByWxName")
    public Map<String, Object> findByWxName(String wxName) {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> map2 = new HashMap<>();
        ArrayList<Object> list = new ArrayList<>();
        User user = userService.findByWxName(wxName);
        //map.put("userInfo", user);
        List<UserCard> userCardList = service.findByUserId(user.getId());
        for (UserCard userCard:userCardList) {
            userCard.setUser(userService.findById(userCard.getUserId()));
        }
        //map.put("user", userCardList);
        list.add(user);
        list.add(userCardList);
        map2.put("users", list);
        return map2;
    }
    @RequestMapping("/findByOpenId")
    public Map<String, Object> findByOpenId(String openId) {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> map2 = new HashMap<>();
        ArrayList<Object> list = new ArrayList<>();
        User user = userService.findByOpenId(openId);
        //map.put("userInfo", user);
        List<UserCard> userCardList = service.findByUserId(user.getId());
        for (UserCard userCard:userCardList) {
            userCard.setUser(userService.findById(userCard.getUserId()));
        }
        //map.put("user", userCardList);
        list.add(user);
        list.add(userCardList);
        map2.put("users", list);
        return map2;
    }
}
